<template>
  <div>
    <Form
      :model="formState"
      :rules="rules"
      ref="formRef"
      layout="horizontal"
      :label-col="{
        style: {
          minWidth: '80px',
          textAlign: 'left',
        },
      }"
    >
      <Form.Item label="任务名称：" name="name">
        <a-input
          :disabled="progressPercent == '100'"
          v-model:value="formState.name"
          placeholder="请输入任务名称"
          @input="formState.name = formState.name.replace(/[^A-Za-z0-9_]/g, '')"
        />
      </Form.Item>

      <Form.Item label="任务描述：" name="code">
        <a-input-number
          :disabled="progressPercent == '100'"
          v-model:value="formState.code"
          :min="1"
          placeholder="请输入任务描述"
          style="width: 100%"
        />
      </Form.Item>

      <Form.Item label="流程组：" name="group">
        <a-select
          :disabled="progressPercent == '100'"
          show-search
          v-model:value="formState.group"
          placeholder="请选择流程组"
          :options="groupOptions"
          :filter-option="filterGroupOption"
          option-label-prop="label"
        />
      </Form.Item>
    </Form>
  </div>
</template>

<script setup>
import { ref, reactive, defineExpose, defineEmits, getCurrentInstance, watch } from 'vue';
import * as http from '@/api/flow/index.js';
import { Form, message } from 'ant-design-vue';
const props = defineProps({
  record: {
    type: Object,
    default: () => ({}),
  },
});
const { proxy } = getCurrentInstance();

const formRef = ref();
const formState = reactive({
  name: '',
  code: null,
  quantity: 1,
  value2: '1',
  value3: '1',
  group: null
});
const rules = reactive({
  name: [
    {
      required: true,
      message: $t('clone.enterCloneName'), // [!code focus]
      trigger: 'blur'
    },
    { min: 2, max: 30, message: $t('clone.lengthLimit'), trigger: 'blur' },
    {
      pattern: /^[A-Za-z0-9_]+$/,
      message: $t('clone.alphanumericOnly'),
      trigger: 'blur'
    }
  ],
  code: [
    { required: true, message: $t('clone.cloneCodeRequired'), trigger: 'blur' },
    { type: 'number', message: $t('clone.numericRequired') }
  ],
  quantity: [
    { required: true, message: $t('clone.fillCloneQuantity') },
    { type: 'number', message: $t('clone.numericRequired') },
    {
      validator: (_, value) =>
        value >= 1 ? Promise.resolve() : Promise.reject($t('clone.quantityMinLimit'))
    }
  ],
  group: [
    { required: true, message: $t('clone.selectGroup'), trigger: 'change' }
  ]
});






const handleSubmit = async () => {
  try {
    await formRef.value.validate();
    const hideLoading = message.loading($t('clone.validating'), 0);
    let params = {
      cloneName: formState.name,
      cloneNumber: formState.code,
      count: formState.quantity,
    }
    http.flowsCloneCheck(params).then(res => {
      hideLoading();
      if (res.code === '000000') {

      }
    });
  } catch (error) {
    console.log('表单验证失败', error);
  }
};

// 所属组选项
const groupOptions = ref([]);
const refreshFlowGroup = () => {
  console.log('刷新流程组');
  const params = {
    type: 'all',
  }
  http.getAllRelFlowGroupInfo(params).then(res => {
    const { code, data, msg, traceId } = res;
    if (code === "000000") {
      console.log(data, 'data');
      if (!data) {
        message.warning(msg);
        groupOptions.value = [];
      } else if (data.length) {
        groupOptions.value = data.map(item => ({
          value: item.id,
          label: item.flow_group_name
        }));
      }
      else {
        message.warning(msg);
        groupOptions.value = [];
      }
    } else {
      message.warning(msg);
    }
  })
}
// 回显
watch(() => props.record, (newVal) => {
  if (newVal) {
    console.log(newVal, 'newVal');
    formState.name = newVal.flowName;
    formState.code = newVal.flowDesc;
    formState.group = newVal.flowGroupId;
  }
}, { immediate: true })






const emit = defineEmits(['close']);
const closes = () => {
  console.log('关闭');
  emit('close');
};
defineExpose({ refreshFlowGroup, closes });
</script>